package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-05-26 13:58
 **/
@RestController
@RequestMapping("/spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     *@描述  新增规格组
     *@参数  [specGroupDTO]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     *@描述   根据分类ID查规格组的集合
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.dto.SpecGroupDTO>>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id")Long id){
        List<SpecGroup> groupList = groupService.lambdaQuery()
                .eq(SpecGroup::getCategoryId, id).list();
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(groupList));
    }
    /**
     *@描述       根据分类ID查找详细的分类组及详细信息
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.item.dto.SpecGroupDTO>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupDetailByCategoryId(@RequestParam("id")Long id){
        List<SpecGroupDTO> groupDTOList = groupService.querySpecGroupDetailByCategoryId(id);
        return ResponseEntity.ok(groupDTOList);
    }
    /**
     *@描述  更新规则参数组
     *@参数  [specGroup]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroup specGroup){
        groupService.updateById(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *@描述    s根据cid, gid, searching 查询规格参数
     *@参数  [cid, gid, searching]
     *@返回值  org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.dto.SpecParamDTO>>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam(value = "categoryId",required = false)Long cid,
                                                                   @RequestParam(value = "groupId",required = false)Long gid,
                                                                   @RequestParam(value = "searching",required = false)Boolean searching){
        List<SpecParam> paramList = paramService.querySpecParams(cid, gid, searching);
        return ResponseEntity.ok(SpecParamDTO.convertEntityList(paramList));
    }

    /**
     *@描述  新增规格参数
     *@参数  [paramDTO]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpcParam(@RequestBody SpecParamDTO paramDTO) {
        paramService.save(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     *@描述   更新规格参数
     *@参数  [paramDTO]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/26
     *@修改人和其它信息
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO paramDTO){
        paramService.updateById(paramDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
